﻿using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Input;
using Microsoft.Extensions.DependencyInjection;
using SimpleTool.Base;
using SimpleTool.Server;
using SimpleTool.TransformComponent;

namespace SimpleTool.ActivityUnit.EchoControl
{
    /// <summary>
    /// BitwiseXorUI.xaml 的交互逻辑
    /// </summary>
    public partial class BitwiseXorUI : EnjoyWindow
    {
        private BitwiseViewModel? viewmodel = null;

        private Ruined? Asbin = null;

        public BitwiseXorUI(string content)
        {
            InitializeComponent();

            viewmodel = new(content);

            DataContext = viewmodel;

            Owner = Application.Current.MainWindow;

            WindowStartupLocation = WindowStartupLocation.CenterOwner;

            Loaded += CompletedLoad;
        }


        public void CompletedLoad(object sender, RoutedEventArgs e)
        {
            Asbin = new Ruined(this, () =>
            {
                viewmodel = null;
                Function.Topmast();
                Unloaded -= CompletedLoad;
            });
        }
    }

    public class BitwiseViewModel : Notify
    {
        private IDenCryptoGraphicServer DecryptServer;

        private string _source = string.Empty;

        private string _bitwise = "结果在这里展示";

        private string _base64 = "base64在这里展示";

        private ICommand? _Cmdbitwise = null;

        public BitwiseViewModel(string source)
        {
            App MyApp = (App)Application.Current;

            DecryptServer = MyApp.ServiceProvider.GetRequiredService<IDenCryptoGraphicServer>();

            Match(source);

            _source = source;
        }

        public string bitwise
        {
            set
            {
                _bitwise = value;
                propertyChangeSend(nameof(bitwise));
            }
            get
            {
                return _bitwise;
            }
        }

        public string base64
        {
            set
            {
                _base64 = value;
                propertyChangeSend(nameof(base64));
            }
            get
            {
                return _base64;
            }
        }

        public string source
        {
            set
            {
                _source = value;
                propertyChangeSend(nameof(source));
            }
            get
            {
                return _source;
            }
        }


        public ICommand Cmdbitwise
        {
            set
            {
                _Cmdbitwise = value;
            }
            get
            {
                _Cmdbitwise ??= new EasyCommand((param) =>
                {
                    if (param is not null and string _content)
                    {
                        Match(_content);
                    }
                }, () => true);
                return _Cmdbitwise;
            }
        }


        public void Match(string source)
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                bitwise = source.GetXORcode();
                base64 = DecryptServer.ENbase64(source);
            });
        }
    }
}
